package 线程池;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class Pool{
    public static MyExecutorPool newCachedThreadPool(int m){
        MyExecutorPool myExecutorPool=new MyExecutorPool(m);
        return myExecutorPool;
    }
}
public class MyExecutorPool {

    //阻塞队列
    BlockingQueue<Runnable> blockingQueue=new LinkedBlockingQueue<Runnable> ();

    public MyExecutorPool(int m){
        //线程池中创建m个线程
        for (int i = 0; i <m ; i++) {
            Thread thread=new Thread(()->{
                //线程执行的任务
                try {
                    while (true){
                        //出队
                        Runnable task=blockingQueue.take();
                        //执行任务
                        task.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        blockingQueue.put(runnable);
    }

}
